package synchronizeddemo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Window2 extends Thread{
	static int ticket = 1;
	
	public Window2(String name) {
		super(name);
	}
	@Override
	public void run() {
		while(ticket <= 20){
			// 同步块:对象（4个线程共享的对象）
			synchronized (Window2.class) {
				if (ticket <= 20) {
					System.out.println(this.getName() + ticket);
					ticket++;
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
public class SaleTicketDemo2 {
	public static void main(String[] args) {
		Window2 w1 = new Window2("A1:");
		Window2 w2 = new Window2("A2:");
		Window2 w3 = new Window2("A3:");
		Window2 w4 = new Window2("A4:");
		
		w1.start();
		w2.start();
		w3.start();
		w4.start();
	}
}
